package me.j360.springboot.simple.dao.impl;

import me.j360.springboot.simple.base.dao.impl.BaseDaoImpl;
import me.j360.springboot.simple.bean.BaseEnum;
import me.j360.springboot.simple.dao.AdminDao;
import me.j360.springboot.simple.entity.Admin;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;


/**
 * Dao实现类 - 管理员
 * ============================================================================
 * 版权所有 2014 。
 * 
 * @author min_xu
 * 
 * @version 0.1 2014-01-20
 * @version 0.2 2014-07-25 update by fallenpanda
 * ============================================================================
 */
@Transactional
@Repository
public class AdminDaoImpl extends BaseDaoImpl<Admin, String> implements AdminDao {
	
	public boolean isExistByUsername(String username) {
		String hql = "from Admin where lower(username) = lower(:username) ";
		//Admin admin = (Admin) getSession().createQuery(hql).setParameter(0, username).uniqueResult();
        Admin admin = (Admin) getSession().createQuery(hql).setParameter("username",username).uniqueResult();
		return (admin != null);
	}
	
	public Admin getAdminByUsername(String username) {
        String hql = "from Admin where lower(username) = lower(:username) ";
        //Admin admin = (Admin) getSession().createQuery(hql).setParameter(0, username).uniqueResult();
        return (Admin) getSession().createQuery(hql).setParameter("username",username).uniqueResult();
		//return (Admin) getSession().createQuery(hql).setParameter(0, username).uniqueResult();
	}
	
	public boolean isExistByUsermobile(String usermobile) {
		String hql = "from Admin where lower(usermobile) = lower(:usermobile) ";
		Admin admin = (Admin) getSession().createQuery(hql).setParameter("usermobile", usermobile).uniqueResult();
		return (admin != null);
	}
	
	public Admin getAdminByUsermobile(String usermobile) {
        String hql = "from Admin where lower(usermobile) = lower(:usermobile) ";
		return (Admin) getSession().createQuery(hql).setParameter("usermobile", usermobile).uniqueResult();
	}
	
	public boolean isExistByUseremail(String useremail) {
		String hql = "from Admin where lower(useremail) = lower(:useremail) ";
		Admin admin = (Admin) getSession().createQuery(hql).setParameter("useremail", useremail).uniqueResult();
		return (admin != null);
	}
	
	public Admin getAdminByUseremail(String useremail) {
		String hql = "from Admin useremail = ? ";
		return (Admin) getSession().createQuery(hql).setParameter(0, useremail).uniqueResult();
	}
	
	public boolean isExistByOpenId(String openId) {
		String hql = "from Admin where lower(openId) = lower(?) ";
		Admin admin = (Admin) getSession().createQuery(hql).setParameter(0, openId).uniqueResult();
		return (admin != null);
	}

    public Admin getWeixinAdminByLastOpenId(String openId){
        String hql = "from Admin where lower(lastOpenId) = lower(:openId) and accountEnum = :accountEnum";
        Admin admin = (Admin) getSession().createQuery(hql).setParameter("openId", openId).setParameter("accountEnum", BaseEnum.AccountEnum.WEIXIN).uniqueResult();
        return admin;
    }
	public Admin getAdminByOpenId(String openId){
		Session session = sessionFactory.openSession();
		Admin admin = null;
		try{
			String hql = "from Admin where openId = ? ";
			admin = (Admin) session.createQuery(hql).setParameter(0, openId).uniqueResult();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return admin;
	}
	
	@Override
	public Admin getAdmin(String username, String password) {
		String hql = "from Admin where (username = :username or usermobile = :username or useremail = :username ) and password = :password ";
		return (Admin) getSession().createQuery(hql).setParameter("username", username).setParameter("password", password).uniqueResult();
	}
	
	// 处理关联，忽略isSystem=true的对象
    @Override
    public void delete(Admin admin) {

        super.delete(admin);
    }
	
	// 处理关联，忽略isSystem=true的对象
	@Override
	public void delete(String id) {
		Admin admin = load(id);
		this.delete(admin);
	}

	// 处理关联，忽略isSystem=true的对象。
	@Override
	public void delete(String[] ids) {
		for (String id : ids) {
			this.delete(id);
		}
	}
	
	//重写保存默认非系统账号/启用/未锁定/未过期/凭证未过期
	@Override
	public String save(Admin admin) {

		admin.setLoginFailureCount(0);
		return super.save(admin);
	}

    //这里会回滚
    @Transactional
    public void saveFall(Admin admin){
        save(admin);
        int i = 1/0;
    }

}